/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Wrapper;

import crawler.GetHTML;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import crawler.UrlList;

/**
 *
 * @author Putra
 */
public class TabloidPulsaWrapper implements Runnable {

    private StringBuffer xmlText = new StringBuffer("<?xml version=\"1.0\"?>");
    private String baseUrl;
    private String xmlFileName;
    private String rootName;
    private String itemName;
    private int maxPage;
    private int minPage;
    private String htmlText;
    private String firstMark;
    private String secondMark;
    private String titleMark;
    
    public TabloidPulsaWrapper(int maxPage,String firstMark,String secondMark,String titleMark) {
        this.maxPage = maxPage;
        this.firstMark = firstMark;
        this.secondMark = secondMark;
        this.titleMark = titleMark;
    }
    
    public TabloidPulsaWrapper(int minPage,int maxPage) {
        this.maxPage = maxPage;
        this.minPage = minPage;
    }    


    private void retrievePages(int maxPage) {
        getXmlText().append("\n" + "<" + getRootName() + ">" + "\n");
        UrlList list = new UrlList(minPage,maxPage);
        for (int i = 0; i < list.listUrl.size(); i++) {
            String pageUrl = list.listUrl.get(i);
            String regex = "\\<div class=\"jrFieldGroup .*?\">";
            String htmlText = getHtmlText(pageUrl);
            String[] itemList = new String[100];
            itemList = htmlText.split(regex);

            getXmlText().append("<" + getItemName() + ">\n");
            ElementDiv div = getTitle(itemList[0]);
            String ruasElement = "<" + div.getLabel() + "><![CDATA[" + div.getValue() + "]]></" + div.getLabel() + ">";
            getXmlText().append("  " + ruasElement + "\n");

            for (int j = 1; j < itemList.length; j++) {
                getXmlText().append(process(1, itemList[j]));
            }
            getXmlText().append("</" + getItemName() + ">\n");
            System.out.println("progress : " + (double) (i + 1) / list.listUrl.size());
        }
        getXmlText().append("</" + getRootName() + ">");
    }

    /*
     process pemecahan div div untuk me-wrap data
     */
    private StringBuffer process(int level, String htmlText) {
        StringBuffer xmlText = new StringBuffer();
        if (level == 1) {

            String startGroupTitle = "<h3 class=\"jrFieldGroupTitle\">";
            String endGroupTitle = "</h3>";
            int startPosGroupTitle = htmlText.indexOf(startGroupTitle);
            int endPosGroupTitle = htmlText.indexOf(endGroupTitle, startPosGroupTitle + 1);
            String groupTitle = htmlText.substring(startPosGroupTitle + startGroupTitle.length(), endPosGroupTitle);
            groupTitle = groupTitle.replaceAll("[\\~#%&*{}/:<>?|\"-]","");
            groupTitle = groupTitle.replaceAll(" ","");
            
            xmlText.append("<" + groupTitle + ">" + "\n" + process(2, htmlText) + "</" + groupTitle + ">" + "\n");
            return xmlText;

        } else if (level == 2) {
            String[] itemList = new String[100];
            String regex = "\\<div class=\"jrFieldRow .*?\">";
            itemList = htmlText.split(regex);
            for (int i = 1; i < itemList.length; i++) {
                ElementDiv div = new ElementDiv();
                //System.out.println(item);
                String item = itemList[i];
                String startLabel = "<div class=\"jrFieldLabel\">";
                String endLabel = "</div>";
                int startPosLabel = item.indexOf(startLabel);
                int endPosLabel = item.indexOf(endLabel, startPosLabel + 1);
                String label = item.substring(startPosLabel + startLabel.length(), endPosLabel);

                String startValue = "<div class=\"jrFieldValue \">";
                String endValue = "</div>";
                int startPosValue = item.indexOf(startValue);
                int endPosValue = item.indexOf(endValue, startPosValue + 1);
                String value = item.substring(startPosValue + startValue.length(), endPosValue);

                div.setLabel(label);
                div.setValue(cleanText(removeTags(value)));
                String ruasElement = "<" + div.getLabel() + "><![CDATA[" + div.getValue() + "]]></" + div.getLabel() + ">";
                xmlText.append("  " + ruasElement + "\n");

            }
            return xmlText;
        }
        return xmlText;
    }

    private ElementDiv getTitle(String htmlText) {
        ElementDiv div = new ElementDiv();
        div.setLabel("title");

        String startTitle = "<span itemprop=\"name\">";
        String endTitle = "</span>";
        int startPosTitle = htmlText.indexOf(startTitle);
        int endPosTitle = htmlText.indexOf(endTitle, startPosTitle + 1);
        String title = "";
        if (startPosTitle != -1 || endPosTitle != -1) {
            title = htmlText.substring(startPosTitle + startTitle.length(), endPosTitle);
        }
        div.setValue(cleanText(removeTags(title)));
        return div;
    }

    private String removeTags(String htmlText) {
        htmlText = htmlText.replaceAll("\\<.*?>", " ");
        htmlText = cleanText(htmlText);
        return htmlText;
    }

    private String cleanText(String text) {
        text = text.replaceAll("\\s+", " ");
        text = text.trim();
        return text;
    }

    private String getHtmlText(String targetUrl) {
        String htmlText = null;
        GetHTML getHTML = new GetHTML("", targetUrl);
        Thread thread = new Thread(getHTML);
        thread.run();
        htmlText = getHTML.getHtmlText();

        return htmlText;
    }

    private void saveToFile() {
        try {
            FileWriter file = new FileWriter(getXmlFileName());
            BufferedWriter out = new BufferedWriter(file);
            out.write(getXmlText().toString());
            out.close();
        } catch (IOException e) {
        }
    }

    
    /**
     * @return the xmlText
     */
    public StringBuffer getXmlText() {
        return xmlText;
    }

    /**
     * @param xmlText the xmlText to set
     */
    public void setXmlText(StringBuffer xmlText) {
        this.xmlText = xmlText;
    }

    /**
     * @return the baseUrl
     */
    public String getBaseUrl() {
        return baseUrl;
    }

    /**
     * @param baseUrl the baseUrl to set
     */
    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    /**
     * @return the xmlFileName
     */
    public String getXmlFileName() {
        return xmlFileName;
    }

    /**
     * @param xmlFileName the xmlFileName to set
     */
    public void setXmlFileName(String xmlFileName) {
        this.xmlFileName = xmlFileName;
    }

    /**
     * @return the rootName
     */
    public String getRootName() {
        return rootName;
    }

    /**
     * @param rootName the rootName to set
     */
    public void setRootName(String rootName) {
        this.rootName = rootName;
    }

    /**
     * @return the itemName
     */
    public String getItemName() {
        return itemName;
    }

    /**
     * @param itemName the itemName to set
     */
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    @Override
    public void run() {
        retrievePages(this.maxPage);
        saveToFile();
    }
}
